package dat255.calendarclock.model.event;

import java.io.Serializable;
import java.util.GregorianCalendar;

import dat255.calendarclock.model.event.interval.Interval;
import dat255.calendarclock.model.event.interval.OneTimeInterval;

/**
 * An master event from which the eventhandler generates all of the events that are shown in the application
 * @author Marcus Hultman, Gabriel Andersson & Oskar Montin
 */
public class Event implements Cloneable, Serializable{

	/**
	 * needed to store in files with objectOutputStream;
	 */
	private static final long serialVersionUID = 6368454006156849980L;
	// Field
	private String title;
	private String description;
	private Interval interval;
	private Category category;
	
	// Constructors
	public Event(final String name, final String description, 
			final GregorianCalendar date) {
		
		this(name, description, date, new OneTimeInterval(date), Category.createCategory());
	}
	
	public Event(final String name, final String description, 
			final GregorianCalendar date, final Category category) {
		this(name, description, date, new OneTimeInterval(date), category);
	}
	
	public Event(final String name, final String description, 
			final GregorianCalendar date, final Interval interval) {
		this(name, description, date, interval, Category.createCategory());
	}
	
	public Event(final String name, final String description, 
			final GregorianCalendar date, final Interval interval, final Category category) {
		
		// Set Title
		if (name != null)
			this.title = name;
		else
			this.title = "Untitled";
		
		// Set Description
		if (description != null)
			this.description = description;
		else
			this.description = "";
		
		// Set intercal
		if (interval != null)
			this.interval = interval;
		else
			this.interval = new OneTimeInterval(new GregorianCalendar());
		
		// Set Category
		if (category != null)
			this.category = category;
		else
			this.category = Category.createCategory();
		
	}
	
	// Getters & Setters

	public String getName() {
		return this.title;
	}
	public void setName(final String name){
		this.title = name;
	}

	public String getDescription() {
		return this.description;
	}
	public void setDescription(final String description){
		this.description = description;
	}

	public Interval getInterval() {
		return this.interval;
	}
	public void setInterval(final Interval newInterval){
		this.interval=newInterval;
	}

	public Category getCategory() {
		return this.category;
	}
	public void setCategory(final Category newCategory){
		this.category=newCategory;
	}
	
	// Public methods
	
	@Override
	public Event clone() {
		return new Event(title, description, interval.getFirst() , interval, category);
	}

	/**
	 * If the event has happened the date is set to has happened already
	 * @param date the date that has happened
	 */
	public void setHasHappened(GregorianCalendar date) {
		interval.setHasHappened(date);
	}

}
